<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Spaghetti Coders</title>
<link rel="stylesheet" type="text/css" href="includes/style.css">
</head>

<body>

<?php

$username = $_SERVER['PHP_AUTH_USER'];

include "includes/lib.php";
include "includes/header.php"; 
include "includes/nav.php";



echo '<div id="wrapper">';
echo '<div id="content">';


if (isset($_GET["action"]) == false) {

	echo "<h3> Active Users </h3><br />";
	list_active_users();
}
else {
	switch($_GET["action"]) {

	case "active_users":
	echo "<h3> Active Users </h3><br />";
		list_active_users();
		break;

	case "inactive_users":
	echo "<h3> Inactive Users </h3><br />";
		list_inactive_users();
		break;

	case "view":
		if (isset($_GET["user_id"])) {
			$user_id=$_GET["user_id"];
			view_user($user_id);
		}
		else {
			echo "Error: No user id provided";
		}
		break;


	case "activate":
		if (isset($_GET["user"])) {
			$user=$_GET["user"];
			if (is_admin($username)){
				activate_user($user);
			}
			else {
				echo "Error: Insufficient priviliges.";
			}
		}
		else {
			echo "Error: No username provided";
		}
		break;

	case "deactivate":
		if (isset($_GET["user"])) {
			$user=$_GET["user"];
			if (is_admin($username)){
				deactivate_user($user);
				discard_all_checkouts($user);
			}
			else {
				echo "Error: Insufficient priviliges.";
			}
		}
		else {
			echo "Error: No username provided";
		}
		break;

	case "create_form":
		if (is_admin($username)) {
			echo '<h3> Create New User </h3><br />';
			echo '<form action ="users.php?action=create_user" method="post">';
			echo 'Username: <input type="text" name="user"><br />';
			echo 'Password: <input type="password" name="pass"><br />';
			echo 'Administrator: <input type="checkbox" name="admin" value=1><br />';
			echo '<input type="Submit" value="Submit">';
			echo '</form>';
	
		}
		else {
			echo "Error: Insufficient priviliges to create users.";
		}
		break;

	case "create_user":
		if (is_admin($username)) {
		
			if (isset($_POST["user"]) && isset($_POST["pass"])) {
				$user = $_POST['user'];
				$pass = $_POST['pass'];
				if (isset($_POST["admin"])) {
					$is_admin = 1;
				}
				else {
					$is_admin = 0;
				}
	
				create_user($user,$pass,$is_admin);
			}
			else {
				echo "Error: Incomplete Information.";
			}
		}
		else {
			echo "Error: Insufficient privileges to create users.";
		}

		break;

	default:
		echo "No such action";
		break;
	}
}
?>	
</div>
</body>
</html>

